package yf.mall.dysdk.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author yxmds
 */
public final class StatisticsUtil {

    private StatisticsUtil() {
    }

    public static void main(String[] args) {
        int inputNumber = 1000; // 要拆分和累加的正整数
        List<Integer> result = splitAndSum(inputNumber);

        // 输出结果
        System.out.print("随机正整数列表：");
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
        System.out.println("累加结果：" + result.stream().limit(result.size() - 1).mapToInt(s -> s).summaryStatistics());
    }

    public static List<Integer> splitAndSum(int number) {
        List<Integer> randomIntegers = new ArrayList<>();
        Random random = new Random();

        int average = number / 24; // 步骤一：计算平均数

        if (average > 5) {
            for (int i = 0; i < 24; i++) {
                int randRange = random.nextInt(10) - 4;
                System.out.println(randRange);
                int randomValue = average + randRange; // 步骤二：加减（0-5）的随机数
                randomIntegers.add(randomValue);
                number -= randomValue;
            }
        } else {
            for (int i = 0; i < 24; i++) {
                randomIntegers.add(average);
            }
        }

        // 确保最后一个数将累加和补足为原正整数
        randomIntegers.set(23, randomIntegers.get(23) + number);

        return randomIntegers;
    }

    public static int sumArray(int[] array) {
        int sum = 0;
        for (int num : array) {
            sum += num;
        }
        return sum;
    }


}
